import axios from 'axios'
import {showNotify,showConfirmDialog} from 'vant';
import { getCookie } from '@/utils/httpParamsHandler';
const service = axios.create({
  baseURL:'https://geo.tst.cqtancheng.com/', //'https://farming.tst.cqtancheng.com/',
 // baseURL:'http://192.168.31.133:8080/', //'https://farming.tst.cqtancheng.com/',
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 50000 // request timeout
})

// request拦截器
service.interceptors.request.use(config => {
  config.headers['Authorization'] = getCookie('userToken') ||'NmE1MDc0MGItZDU5OC00MjZiLTljYWUtZjdjNjY1MzRkMTJk'
  config.headers['Token'] = getCookie('userToken') || 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyTmFtZSI6IjEzNjI4NDY2MTY2IiwiZXhwIjoxNzIyNDc5NTI4LCJ1c2VySWQiOiI5OCJ9.kEOW1s6cO3AKuVHL6ycuOh1LgIlwy_qWwjL_lvWxHaU'
  config.headers['X-Request-Id'] = 1

  return config
}, error => {
  Promise.reject(error)
})

// 响应拦截
service.interceptors.response.use(res => {
  if(!res.data?.success && res.data?.message=="需要登录"){
    showConfirmDialog({
      title: '提示',
      message:'长时间未操作，请重新登录?',
    }).then(async () => {
      wx.miniProgram.navigateBack()
    })
  }
  return res
}, error => {
  showNotify({type: 'danger', message: '无法连接网络，请检查您的网络连接' });
  if(error.response?.status == 500){
    return Promise.resolve(error.response)
  }else{
    return Promise.resolve(error.response)
  }
})

export default service
